package com.itheima.health.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @author ：seanyang
 * @date ：Created in 2019/6/12
 * @description ：套餐Controller
 * @version: 1.0
 */
@RestController
@RequestMapping("/mobile/setmeal")
public class SetmealController {

	@Autowired
	private JedisPool jedisPool;

	@Reference
	private SetmealService setmealService;

	/**
	 * 获取套餐列表数据
	 * @return
	 */
	@RequestMapping("/getSetmeal")
	public Result getSetmeal(){
		try{
			Jedis jedis = jedisPool.getResource();
			if (jedis.get("setmealList") == null) {
				List<Setmeal> setmealList = setmealService.findAll();
				String setmealListJosn = JSON.toJSONString(setmealList);
				jedis.setex("setmealList",30*60, setmealListJosn);
			}
			String setmealList = jedis.get("setmealList");
			Object parse = JSON.parse(setmealList);
			return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS,parse);
		}catch(Exception e){
			e.printStackTrace();
			return new Result(false,MessageConst.QUERY_SETMEAL_FAIL);
		}
	}

	@RequestMapping("/findById")
	public Result findById(Integer id){
		try{
			Jedis jedis = jedisPool.getResource();

			if (jedis.get("setmeal"+id) == null) {

				Setmeal setmeal = setmealService.findById(id);
				String setmealJson = JSON.toJSONString(setmeal);
				jedis.setex("setmeal"+id,30*60,setmealJson);
			}
			String setmealJson = jedis.get("setmeal"+id);
			Object parse = JSON.parse(setmealJson);
			return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,parse);
		}catch(Exception e){
			e.printStackTrace();
			return new Result(false,MessageConst.QUERY_SETMEAL_FAIL);
		}
	}


}
